package com.bignerdranch.android.ecar.Fragment;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bignerdranch.android.ecar.Activity.BlogActivity;
import com.bignerdranch.android.ecar.Activity.FaultActivity;
import com.bignerdranch.android.ecar.Activity.RegisterActivity;
import com.bignerdranch.android.ecar.Activity.SettingActivity;
import com.bignerdranch.android.ecar.Activity.UserInfoActivity;
import com.bignerdranch.android.ecar.R;
import com.bignerdranch.android.ecar.Utils.URLBank;
import com.facebook.drawee.view.SimpleDraweeView;

/**
 * Created by Administrator on 2016/7/18.
 */
public class MineFragment extends Fragment implements View.OnClickListener{
    public static final int  HEAD_REQUEST = 0;

    private SimpleDraweeView head;
    private TextView login;
    private RelativeLayout main_1;
    private RelativeLayout mine_2;
    private RelativeLayout mine_3;
    private RelativeLayout mine_4;
    private RelativeLayout mine_5;
    SharedPreferences sp;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        initView(view);
//        updateUI();
        sp = getActivity().getSharedPreferences("user",Activity.MODE_PRIVATE);
        return view;
    }


    private void updateUI(){

//        Log.e("====status=",sp.getBoolean("login_status",false)+"");
        if (sp.getBoolean("login_status",false)){
            String img_name = sp.getString("headPic",null);
            String url = URLBank.teacher_url+URLBank.upload_url+img_name;
//            Log.e("====url=",url);
            Uri uri = Uri.parse(url);
//            Log.e("==uri===",uri.toString());
            login.setText(sp.getString("userName","xxxxxxxx"));
            head.setImageURI(uri);
        }else {
            head.setImageResource(R.drawable.head);
            login.setText(sp.getString("userName","xxxxxxxx"));
        }

    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK&&requestCode == HEAD_REQUEST){
            Log.e("======",data.getStringExtra("userName"));
            Log.e("======",data.getBooleanExtra("login_status",false)+"");
            login.setText(sp.getString("userName","xxxxxxxx"));
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        login.setText(sp.getString("userName","xxxxxxxx"));
        updateUI();
    }

    private void initView(View view) {
        head = (SimpleDraweeView) view.findViewById(R.id.head);
        head.setOnClickListener(this);
        login = (TextView)view.findViewById(R.id.login);
        login.setOnClickListener(this);
        main_1 = (RelativeLayout) view.findViewById(R.id.main_1);
        main_1.setOnClickListener(this);
        mine_2 = (RelativeLayout) view.findViewById(R.id.mine_2);
        mine_2.setOnClickListener(this);
        mine_3 = (RelativeLayout) view.findViewById(R.id.mine_3);
        mine_3.setOnClickListener(this);
        mine_4 = (RelativeLayout) view.findViewById(R.id.mine_4);
        mine_4.setOnClickListener(this);
        mine_5 = (RelativeLayout) view.findViewById(R.id.mine_5);
        mine_5.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.head:
                if (sp.getBoolean("login_status",false)){
                    Intent intent3 = new Intent(getActivity(), UserInfoActivity.class);
                    startActivity(intent3);

                }else {
                    Intent intent = new Intent(getActivity(), RegisterActivity.class);
                    startActivityForResult(intent,HEAD_REQUEST);
                }

                break;
            case R.id.login:
                /*if (sp.getBoolean("login_status",false)){
                    Intent intent3 = new Intent(getActivity(), UserInfoActivity.class);
                startActivity(intent3);*/

             /*   }else {
                    Intent intent = new Intent(getActivity(), RegisterActivity.class);
                    startActivityForResult(intent,HEAD_REQUEST);
                }*/
                break;
            case R.id.main_1:
                Intent intent1 = new Intent(getActivity(), FaultActivity.class);
                startActivity(intent1);
                break;
            case R.id.mine_2:
                break;
            case R.id.mine_3:

                Intent intent4 = new Intent(getActivity(), BlogActivity.class);
                startActivity(intent4);

                break;
            case R.id.mine_4:
                break;
            case R.id.mine_5:
                Intent intent5 = new Intent(getActivity(), SettingActivity.class);
                startActivity(intent5);
                break;

        }
    }


}
